﻿using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
using DotNet.Common;

namespace System.Windows.Forms
{
	/// <summary>
	/// 嵌入控件面板类
	/// </summary>
	[System.Drawing.ToolboxBitmap(typeof(Panel))]
	public partial class MDIPanel : Panel
	{
		public event VoidInvoker Loaded;

		Control m_lastShowControl = null;

		public MDIPanel()
		{
			InitializeComponent();
			this.Resize += new EventHandler(MDIPanel_Resize);
		}

		public MDIPanel(IContainer container)
		{
			container.Add(this);

			InitializeComponent();
			this.Resize += new EventHandler(MDIPanel_Resize);
		}

		/// <summary>
		/// 调整控件大小
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void MDIPanel_Resize(object sender, EventArgs e)
		{
			foreach (Control subCtl in this.Controls)
			{
				if (subCtl is Form)
				{
					ResizeForm(subCtl as Form);
				}
			}
		}

		/// <summary>
		/// 调整窗体大小居中
		/// </summary>
		/// <param name="subForm"></param>
		void ResizeForm(Form subForm)
		{
			if (subForm.Width < this.Width)
			{
				subForm.Left = ((this.Width - subForm.Width) >> 1);
			}
			else
			{
				subForm.Left = 0;
			}

            if((subForm.Anchor & (AnchorStyles.Bottom | AnchorStyles.Top)) == (AnchorStyles.Bottom | AnchorStyles.Top))
            {
                subForm.Height = this.Height;
            }
            else
            {
                if (subForm.Height < this.Height)
                {
                    subForm.Top = ((this.Height - subForm.Height) >> 1);
                }
                else
                {
                    subForm.Top = 0;
                }
            }
		}

		/// <summary>
		/// 添加控件数组
		/// </summary>
		/// <param name="subControls"></param>
		public void Load(params Control[] subCtls)
		{
			base.Controls.Clear();

			foreach (Control subCtl in subCtls)
			{
				if (subCtl == null)
					continue;

				if (subCtl is Form)
				{
					Form subForm = (subCtl as Form);
					subForm.FormBorderStyle = FormBorderStyle.None;
					subForm.BackColor = this.BackColor;
					subForm.TopLevel = false;

					ResizeForm(subForm);
				}
				else
				{
					if (subCtl is TextBoxBase)
					{
						(subCtl as TextBoxBase).Multiline = true;
						if (subCtl is TextBox)
						{
							(subCtl as TextBox).ScrollBars = ScrollBars.Both;
						}
						else if (subCtl is RichTextBox)
						{
							(subCtl as RichTextBox).ScrollBars = RichTextBoxScrollBars.Both;
						}
					}

					subCtl.Width = this.Width;
					subCtl.Height = this.Height;

					subCtl.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top);
				}

				subCtl.Visible = false;
			}

			base.Controls.AddRange(subCtls);

			if (Loaded != null)
				Loaded();
		}

		/// <summary>
		/// 显示子控件
		/// </summary>
		/// <param name="subControl"></param>
		public void Show(Control subControl)
		{
			if (this.Controls.Contains(subControl))
			{
				if (m_lastShowControl != null)
				{
					m_lastShowControl.Visible = false;
				}
				subControl.Visible = true;

				m_lastShowControl = subControl;
			}
		}
	}
}
